package com.mktravel.thailand

class Person {

	transient springSecurityService

	String username
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
	String firstName
	String lastName
	
	// khong bat buoc
	String fullName
	String street1
	String street2
	String city
	String state
	String country
	String zip
	String mobilePhone
	String homePhone
	
	static constraints = {
		username blank: false, unique: true
		password blank: false
		
		street1 nullable:true
		street2 nullable:true
		city nullable:true
		state nullable:true
		country nullable:true
		zip nullable:true
		mobilePhone nullable:true
		homePhone nullable:true
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		PersonRole.findAllByPerson(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
